###############################################
#                                             #
#     State Survey Treatment Effects          #
#     Created by Mac Lockhart Nov 30 2022     #
#     Updated Dec 1 2022                      #
#                                             #
###############################################

library(tidyverse)
library(data.table)
library(modelsummary)
library(dotwhisker)
#Read in most recent version of cleaned data using:
#Clean_National_Sample.R
if(Sys.info()[7] == "maclockhart"){
  setwd("/Users/maclockhart/Dropbox/School/RA Work/'22 Seth & Thad/MIT Evolving Election Administration Landscape")
}


for(state in c("Colorado", "Georgia", "LA", "Texas")){
  DF<- fread(paste0("Survey Data/", state, " Recoded.csv"))
  DF<-DF[bot==0]
  #Code outcome variables as change from pre-treatment values
  DF$ownstate <- DF$ownstatepost-DF$ownstatepre
  DF$otherstates <- DF$otherstatepost - DF$otherstatepre
  DF$votefraud <- DF$votefraudpost - DF$votefraudpre
  DF$officialfraud <- DF$officialfraudpost - DF$officialfraudpre
  DF$vote2024 <- DF$vote2024post - DF$vote2024pre
  
  #relevel treatment variable in Colorado to compare treatments to the control
  if(state=="Colorado"){DF$state_treatment <- factor(DF$state_treatment, levels=c("Control", "Colorado - Absentee Voting", "Colorado - Facts"))}
  
  #Pooled treatment effects by video
  m1.1 <- lm(ownstate ~ d_state_treatment, DF, weights=the.wts)
  m1.2 <- lm(otherstates ~ d_state_treatment, DF, weights=the.wts)
  m1.3 <- lm(votefraud ~ d_state_treatment, DF, weights=the.wts)
  m1.4 <- lm(officialfraud ~ d_state_treatment, DF, weights=the.wts)
  m1.5 <- lm(vote2024 ~ d_state_treatment, DF, weights=the.wts)
  
  #Separate treatment effects by video
  m2.1 <- lm(ownstate ~ state_treatment, DF, weights=the.wts)
  m2.2 <- lm(otherstates ~ state_treatment, DF, weights=the.wts)
  m2.3 <- lm(votefraud ~ state_treatment, DF, weights=the.wts)
  m2.4 <- lm(officialfraud ~ state_treatment, DF, weights=the.wts)
  m2.5 <- lm(vote2024 ~ state_treatment, DF, weights=the.wts)
  
  #Output results in Regression tables
  binary_models <- list("Trust Own State" = m1.1,
                        "Trust Other States" = m1.2,
                        "Vote Fraud Belief" = m1.3,
                        "Officials Fraud Belief" = m1.4,
                        "2024 Vote Intent" = m1.5)
  modelsummary(binary_models, output = paste0("Mac and Jen Results/Treatment Effects/",state,"_binary.docx"),
               #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
               statistic = 'std.error', stars=T,
               coef_omit = "Intercept", gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
  full_models <- list("Trust Own State" = m2.1,
                      "Trust Other States" = m2.2,
                      "Vote Fraud Belief" = m2.3,
                      "Officials Fraud Belief" = m2.4,
                      "2024 Vote Intent" = m2.5)
  modelsummary(full_models, output = paste0("Mac and Jen Results/Treatment Effects/",state,"_categorical.docx"),
               #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
               statistic = 'std.error', stars=T,
               coef_omit = "Intercept", gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
  }
